import React from "react";
import classNames from 'classnames';
import Input from './input';
import Button from '../Button';

import {omit} from '../../util/omit';
import {splitObject} from '../../util/splitObject';

export default class Search extends React.Component {

    static defaultProps = {
        prefixCls:"fly-input-search",
        btnValue:"搜索"
    };

    onSearch = ()=>{
        const {onSearch} = this.props,
              value = this.input.refs.input.value;

        if(onSearch){ onSearch(value) }
    }

    render() {
        const [{style,btnValue,className,prefixCls},others] = splitObject(this.props,[
            "style","btnValue","className","prefixCls"
        ]);

        void delete others.onSearch;

        const wrapperCls =  classNames({
            [`${prefixCls}-wrapper`] : true
        },className);

        return <div className={wrapperCls} style={style}>
            <Input
                {...others}
                className={prefixCls}
                onPressEnter={this.onSearch}
                ref={(input)=>{this.input = input}}
            />
            <Button
                onClick={this.onSearch}
            >
                {btnValue}
            </Button>
        </div>
    }
}
